package com.juku2024.juku

import android.os.Bundle
import android.view.View
import android.widget.TextView
import com.blankj.utilcode.util.PathUtils
import com.juku2024.juku.base.BaseActivity
import com.juku2024.juku.callback.OnNoDoubleClickListener
import com.juku2024.juku.model.DownloadPathEvent
import com.juku2024.juku.tools.AppUtils
import com.juku2024.juku.tools.FileUtils
import com.juku2024.juku.tools.RxUtils
import com.juku2024.juku.tools.SPUtil
import com.wanban.download.WBDownloadUtils
import org.greenrobot.eventbus.EventBus

class PathActivity : BaseActivity(), OnNoDoubleClickListener {

    private lateinit var outStoreRl: View
    private lateinit var phoneRl: View
    private lateinit var storeInfoTv: TextView
    private lateinit var storeInfoPath: TextView
    private lateinit var phoneSelectCb: View
    private lateinit var outSelectCb: View
    private lateinit var outInfoTv: TextView

    //是否准备选择内部存储
    private var isSelectedInner: Boolean = false

    override fun getLayoutResId(): Int {
        return R.layout.activity_data_path
    }

    override fun initView() {
        outStoreRl = findViewById(R.id.out_store_rl)
        phoneRl = findViewById(R.id.phone_rl)
        storeInfoTv = findViewById(R.id.store_info_tv)
        storeInfoPath = findViewById(R.id.store_info_path)
        phoneSelectCb = findViewById(R.id.phone_select_cb)
        outSelectCb = findViewById(R.id.out_select_cb)
        outInfoTv = findViewById(R.id.out_info_tv)

        if (AppUtils.isStorageTf()) {
            val isUseSdCard = WBDownloadUtils.isUseSdcardDownload()
            if (isUseSdCard) {
                setOuterPathUI()
            } else {
                // 让内部存储变成默认状态
                setInnerPathUI()
            }
        } else {
            // 隐藏外部存储ui
            outStoreRl.visibility = View.GONE
            // 让内部存储变成默认状态
            setInnerPathUI()
        }
    }

    override fun bindEvent() {
        super.bindEvent()
        phoneRl.setOnClickListener(this)
        outStoreRl.setOnClickListener(this)
    }

    override fun init(savedInstanceState: Bundle?) {
        // 初始化内部存储的空间
        val appDownloadFile = PathUtils.getExternalStoragePath()
        val totalInternalMemorySize = FileUtils.formatFileSize(FileUtils.getFileTotalSize(appDownloadFile), true)
        val availableInternalMemorySize = FileUtils.formatFileSize(FileUtils.getAvailableExternalMemorySize(appDownloadFile), true)
        storeInfoTv.text = String.format(getString(R.string.data_path_info), totalInternalMemorySize, availableInternalMemorySize)
        if (BuildConfig.DEBUG) {
            storeInfoPath.visibility = View.VISIBLE
            storeInfoPath.text = appDownloadFile
        }

//        if (AppUtils.isStorageTf()) {
//            val sdcardDownloadPath = DSPUtils.instance.getSdcardDownloadPath()?:return
//            val totalSdcardSize = FileUtils.formatFileSize(FileUtils.getFileTotalSize(sdcardDownloadPath), true)
//            val availableSdcardMemorySize = FileUtils.formatFileSize(FileUtils.getAvailableExternalMemorySize(sdcardDownloadPath), true)
//            outInfoTv.text = String.format(getString(R.string.data_path_info), totalSdcardSize, availableSdcardMemorySize)
//            if (BuildConfig.DEBUG) {
//                storeOutPath.visibility = View.VISIBLE
//                storeOutPath.text = DSPUtils.instance.getSdcardDownloadPath()
//            }
//        }
    }


    override fun onNoDoubleClick(v: View?) {
        super.onNoDoubleClick(v)
        when (v?.id) {
            R.id.backLayout -> finish()
            R.id.phone_rl -> {
                if (isSelectedInner) return
                // 选中内部存储，不实用sdcard
                checkSavePath(false)
            }
            R.id.out_store_rl -> {
                if (!isSelectedInner) return
                // 选中sdcard存储，不实用inner
                checkSavePath(true)
            }
        }
    }

    // 先判断是否有正在下载的视频，有提示无法更换，否则去更换
    private fun checkSavePath(isSdcard: Boolean) {

    }

    private fun onPathChoose(isSdcard: Boolean) {
        isSelectedInner = !isSdcard
        WBDownloadUtils.setUserSdcardDownload(isSdcard)
        if (isSelectedInner) {
            setInnerPathUI()
        } else {
            setOuterPathUI()
        }
        EventBus.getDefault().post(DownloadPathEvent())
    }

    private fun setInnerPathUI() {
        isSelectedInner = true
        phoneSelectCb.setBackgroundResource(R.drawable.ic_checked)
        outSelectCb.setBackgroundResource(R.drawable.ic_check_normal)
    }

    private fun setOuterPathUI() {
        isSelectedInner = false
        phoneSelectCb.setBackgroundResource(R.drawable.ic_check_normal)
        outSelectCb.setBackgroundResource(R.drawable.ic_checked)
    }


    private fun showDownloadingDialog() {
        showErrorDialog(
            getString(R.string.switch_path_msg),
            getString(R.string.confirm),
            {
                hideErrorDialog()
            }
        )
    }

}